package org.idea.spring.ioc.container;

import org.idea.spring.ioc.bean.Person;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * @link {ApplicationContext} ApplicationContex 作为ioc容器
 * @Author idea
 * @Date created in 9:54 下午 2020/5/3
 */
public class ApplicationContextAsIocContainer {

    public static void main(String[] args) {
        //通过注解的方式来加载上下文
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.register(ApplicationContextAsIocContainer.class);
        //注册上下文
        annotationConfigApplicationContext.refresh();
        lookupForBean(annotationConfigApplicationContext);
    }

    /**
     * 用于测试的一个bean对象
     *
     * @return
     */
    @Bean
    public Person person(){
        Person person = new Person();
        person.setId(1);
        person.setName("idea");
        return person;
    }

    public static void lookupForBean(BeanFactory beanFactory){
        Person person = beanFactory.getBean(Person.class);
        System.out.println(person);
    }

}
